<?php
/**
 * File from WEBO Video Transmuter, WEBO Software (http://www.webogroup.com/)
 * Provides video convert interface.
 *
 **/
class webo_video_transmuter_converter {
/* constructor */
	function webo_video_transmuter_converter ($options) {
		$this->basepath = dirname(__FILE__) . '/../';
		$this->options = $options;
	}
/* convert video to mp4/ogg */
	function convertVideo ($file, $mode = 0) {
		$file_base = preg_replace("!\.[^\.]+$!is", "", $file);
/* need to somehow define from video */
		$width = 320;
		$height = 240;
		$state_file = $this->options['videodir'] . $file_base . '.state';
/* get bitrates */
		$output = @shell_exec($this->options['ffmpeg'] . ' -i ' . $this->options['videodir'] . $file . ' 2>&1');
		$vbitrate = preg_replace("!.*Video:.*?([0-9]+)\skb/s.*!is", "$1", $output);
		$abitrate = preg_replace("!.*Audio:.*?([0-9]+)\skb/s.*!is", "$1", $output);
		switch ($mode) {
/* convert file to m4v */
			case 3:
				$target = $this->options['videodir'] .
					'mp4/' . $file_base . '.m4v';
				if ($file_base . '.m4v' == $file) {
					@copy($this->options['videodir'] . $file, $this->options['videodir'] . 'mp4/' . $file);
				} else {
					@shell_exec($this->options['ffmpeg'] .
					' -y -i -ac 2 "' .
					$this->options['videodir'] . $file .
					'" -ab ' .
					$abitrate .
					'k -vb '.
					$vbitrate .
					'k -vcodec mpeg4 -acodec libfaac -threads 2 "' . $target . '"');
/* try lower audio bitrate */
					if (!@filesize($target)) {
						@shell_exec($this->options['ffmpeg'] .
						' -y -i -ac 2 "' .
						$this->options['videodir'] . $file .
						'" -vcodec mpeg4 -acodec libfaac -threads 2 -ab 16k "' . $target . '"');
					}
				}
				if (@filesize($target)) {
					@file_put_contents($state_file, 4);
				} else {
					@file_put_contents($state_file, 0);
				}
				break;
/* convert file to mp4 */
			case 2:
				$target = $this->options['videodir'] .
					'mp4/' . $file_base . '.mp4';
				if ($file_base . '.mp4' == $file) {
					@copy($this->options['videodir'] . $file, $this->options['videodir'] . 'mp4/' . $file);
				} else {
					@shell_exec($this->options['ffmpeg'] .
					' -y -i -ac 2 "' .
					$this->options['videodir'] . $file .
					'" -ab ' .
					$abitrate .
					'k -vb '.
					$vbitrate .
					'k -vcodec libx264 -coder 0 -bf 0 -flags2 "-wpred-dct8x8+mbtree" -wpredp 0 -acodec libfaac -threads 2 "' . $target . '"');
/* try lower audio bitrate */
					if (!@filesize($target)) {
						@shell_exec($this->options['ffmpeg'] .
						' -y -i -ac 2 "' .
						$this->options['videodir'] . $file .
						'" -vcodec libx264 -coder 0 -bf 0 -flags2 "-wpred-dct8x8+mbtree" -wpredp 0 -acodec libfaac -threads 2 -ab 16k "' . $target . '"');
					}
				}
				if (@filesize($target)) {
					@file_put_contents($state_file, 4);
				} else {
					@file_put_contents($state_file, 0);
				}
				break;
/* convert file to ogg */
			case 1:
				$target = $this->options['videodir'] .
					'theora/' . $file_base . '.ogv';
				@shell_exec($this->options['ffmpeg'] .
				' -y -i -ac 2 "' .
				$this->options['videodir'] . $file .
				'" -ab ' .
				$abitrate .
				' -vb '.
				$vbitrate .
				' -vcodec libtheora -acodec libvorbis -threads 2 "' . $target . '"');
/* try lower audio bitrate */
				if (!@filesize($target)) {
					@shell_exec($this->options['ffmpeg'] .
					' -y -i -ac 2 "' .
					$this->options['videodir'] . $file .
					'" -vcodec libtheora -acodec libvorbis -threads 2 -ab 16k "' . $target . '"');
				}
/* try to exclude vorbis codec */
				if (!@filesize($target)) {
					@shell_exec($this->options['ffmpeg'] .
					' -y -i -ac 2 "' .
					$this->options['videodir'] . $file .
					'" -vcodec libtheora -threads 2 "' . $target . '"');
				}
				if (@filesize($target)) {
					@file_put_contents($state_file, 4);
				} else {
					@file_put_contents($state_file, 0);
				}
				break;
/* make screenshot */
			default:
				$target = $this->options['videodir'] .
					'thumb/' . $file_base . '.jpg';
				@shell_exec($this->options['ffmpeg'] .
				' -y -i "' .
				$this->options['videodir'] . $file .
				'" -ss 4 -vframes 1 -an -f image2 -s ' .
				$width . 'x' . $height . 
				' "' . $target . '"');
				if (@filesize($target)) {
					@file_put_contents($state_file, 4);
				} else {
					@file_put_contents($state_file, 0);
				}
				break;
		}
	}
/* upload file to process */
	function uploadVideo () {
		if (!empty($_FILES['wvt_file']['tmp_name'])) {
			$file = str_replace(array('#', '$', '%', '^', '&', '*', '?', '/', ' ', '!', '`', '\'', '"'), array('N', 'S', 'P', '', 'A', 'X', 'Q', '', '', '', '', ''), $_FILES['wvt_file']['name']);
			$chunks = explode('.', $file);
			$ext = strtolower(array_pop($chunks));
/* simple test for extension */
			if (in_array($ext, array('032', '263', '264', '3g2', '3gp', '3gp', '3gp2', '3gpp', '3gpp2', '3mm', '3p2', '44', '4xm', '603', '60d', '800', 'acw', 'ad', 'adr', 'afl', 'am2', 'am4', 'am5', 'am6', 'am7', 'amc', 'amv', 'aps', 'apz', 'ara', 'arf', 'asd', 'ask', 'ass', 'ast', 'asx', 'aut', 'aut', 'avc', 'avd', 'avf', 'avi', 'avl', 'avr', 'avs', 'avs', 'awlive', 'awm', 'axm', 'az', 'badongo', 'bam', 'bay', 'bbv', 'bdm', 'bdmv', 'bik', 'bix', 'biz', 'bnk', 'box', 'box', 'bpj', 'bs4', 'bsf', 'bts', 'bub', 'bup', 'buy', 'bvz', 'c2r', 'cak', 'cam', 'cam', 'cam', 'camproj', 'camrec', 'camthtr', 'ccp', 'ccr', 'cct', 'cin', 'cine', 'clpi', 'cmv', 'cor', 'cpk', 'crv', 'ctd', 'ctd', 'cvc', 'd2v', 'd3v', 'dal', 'daproj', 'dat', 'dav', 'db2mov', 'dc8', 'dce', 'demo4', 'dif', 'dif', 'dir', 'div', 'divx', 'dlx', 'dmb', 'dmf', 'dmo', 'dmsm', 'dmss', 'dof', 'dpd', 'dpg', 'dps', 'drt', 'dsf', 'dsm', 'dsy', 'dtv', 'dv', 'dv-avi', 'dvdmedia', 'dvdrip', 'dvi', 'dvm', 'dvr', 'dvr', 'dvr', 'dvr-ms', 'dvsd', 'dvt', 'dvx', 'dwz', 'dxa', 'dxr', 'ecf', 'enc', 'epb', 'epm', 'es3', 'eti', 'ev2', 'eva', 'evo', 'eye', 'eye', 'ezp', 'f4v', 'fbr', 'fbr', 'fbz', 'fcm', 'fcp', 'film', 'film', 'flc', 'flc', 'flh', 'fli', 'flic', 'fli_', 'flm', 'flm', 'flux', 'flv', 'flvat', 'fmc', 'fmf', 'fmv', 'fr', 'fre', 'fvt', 'g64', 'gfp', 'gl', 'gmm', 'gmt', 'grasp', 'gtp', 'gts', 'gvi', 'gxf', 'h-263', 'h.263', 'h261', 'h263', 'h263+', 'h264', 'h3r', 'h4v', 'h64', 'hav', 'hdmov', 'hgd', 'hkm', 'hmt', 'hmv', 'hnm', 'hs', 'htd', 'htp', 'hup', 'icv', 'ifo', 'iis', 'imovieproj', 'imovieproject', 'intl', 'ipf', 'ipr', 'ipx', 'ismv', 'iva', 'ivf', 'ivm', 'ivr', 'ivs', 'ivu', 'izz', 'izzy', 'jpv', 'jts', 'k3g', 'l32', 'las', 'lit', 'lmv', 'lrec', 'lsf', 'lza', 'm-jpeg', 'm15', 'm1pg', 'm1s', 'm1v', 'm21', 'm2p', 'm2s', 'm2t', 'm2ts', 'm2v', 'm3a', 'm4e', 'm4f', 'm4u', 'm4v', 'm65', 'mad', 'mbv', 'mcf', 'mcg', 'mcv', 'met', 'mfp', 'mfv', 'mgv', 'mio', 'mj2', 'mjp2', 'mjpeg', 'mjpg', 'mk3d', 'mks', 'mkv', 'mkv3D', 'mmm', 'mmm', 'mmp', 'mms', 'mmv', 'mnv', 'mod', 'moff', 'moo', 'moov', 'mov', 'movie', 'mp', 'mp0', 'mp21', 'mp2v', 'mp4', 'mp4', 'mp4', 'mp4v', 'mpb', 'mpcpl', 'mpe', 'mpeg', 'mpeg1', 'mpeg2', 'mpeg4', 'mpg', 'mpg', 'mpg2', 'mpg4', 'mpgindex', 'mpj', 'mpj', 'mps', 'mpv', 'mpv2', 'mqv', 'mrd', 'msh', 'mswmm', 'mt2s', 'mts', 'mtv', 'mv', 'mv1', 'mv4', 'mv85', 'mv93', 'mv95', 'mvd', 'mvd', 'mve', 'mve', 'mvf', 'mvp', 'mvw', 'mvy', 'mxm', 'mxv', 'mys', 'n3r', 'nde', 'noa', 'npv', 'nut', 'nuv', 'nvavi', 'nvc', 'of', 'ogm', 'ogv', 'ogx', 'okozo', 'olproj', 'omf', 'otrkey', 'p64', 'pa', 'pa', 'pae', 'par', 'pcf', 'pds', 'pjr', 'plb', 'pmf', 'pmp', 'ppj', 'prel', 'pro', 'prproj', 'psp', 'pva', 'pvr', 'px', 'pxm', 'pxv', 'pyv', 'qt', 'qtc', 'qtm', 'qtv', 'qtvr', 'ratdvd', 'rax', 'rca', 'rcproject', 'rdg', 'rec', 'rec', 'rec', 'rf', 'rm', 'rmd', 'rmf', 'rms', 'rmvb', 'roq', 'rpl', 'rpl', 'rts', 'rts', 'rtsl', 'rtsp', 'rtv', 'rum', 'rv', 'rvl', 'rvx', 'san', 'sbsc', 'sbst', 'scc', 'sce', 'scm', 'scm', 'scn', 'scr', 'scs', 'scx', 'sdr2', 'sdv', 'sec', 'sec', 'seq', 'sfvidcap', 'sgv', 'sgvx', 'sky', 'slc', 'sm', 'smi', 'smk', 'smpg', 'smv', 'smv', 'smv', 'smz', 'snd', 'sol', 'ssif', 'ssm', 'ssw', 'st4', 'stk', 'str', 'stu', 'stx', 'svd', 'svi', 'swc', 'swf', 'swi', 'swt', 'swt', 'swt', 'swz', 'tda3mt', 'tdt2', 'tgv', 'thm', 'thp', 'tir', 'tivo', 'tix', 'tmf', 'tmi', 'tmv', 'tod', 'tp', 'tp0', 'tpd', 'tridefmovie', 'trn', 'trp', 'trp', 'trt', 'ts', 'ts4', 'tstream', 'tts', 'tv', 'tvml', 'tvod', 'tvs', 'tvv', 'tx3g', 'urc', 'usm', 'uvcab', 'uvf', 'uvnts', 'uvs', 'uvseg', 'v', 'vbs', 'vc1', 'vcd', 'vcl', 'vcm', 'vcp', 'vcs', 'vcv', 'vdf', 'vdm', 'vdr', 'vdx', 'veg', 'vem', 'vep', 'vep4', 'vf', 'wmv'))) {
				@move_uploaded_file($_FILES['wvt_file']['tmp_name'], $this->options['videodir'] . $file);
				return $file;
			} else {
				return 'FILETYPE_NOT_SUPPORTED';
			}
		} elseif (!empty($_POST['wvt_url'])) {
			$file = time();
			while (is_file($this->options['videodir'] . $file . '.state')) {
				$file++;
			}
			if (strpos($_POST['wvt_url'], 'youtube.com') !== 'false') {
				$cookie = "demographics=0cd7ea36be90ceba9c270b0f8f67c85ee3QGAAAAZ2VuZGVyTnQDAAAAYWdlTjA=; VISITOR_INFO1_LIVE=nsWt8a2OEEM; use_hitbox=72c46ff6cbcdb7c5585c36411b6b334edAEAAAAw; watched_video_id_list_2nxxT=20fe5d6ef738824eb066ecfa8d26af27Ww8AAABzCwAAAF90aDBzLUxxZkVrcwsAAAB2cmpNZ2g5cVE2VXMLAAAAN1hDdDl0MUNlRk1zCwAAAFNDYTZzaUFBZ19ZcwsAAABXVHBsZHEzbXlWMHMLAAAATGJObDNKOEhYdzRzCwAAAF96QkE0R3FLM2xJcwsAAABxS3NieEF0eDZTa3MLAAAAaTNuYWpCTUoyc01zCwAAAHNYTlhZRW53WXpvcwsAAABydDVhVWRpakFOOHMLAAAAc0NGeWdKTHU1allzCwAAAElKMENXLW10UjlFcwsAAABfdXZnbTNzTkp5c3MLAAAAMUczS3l1X1VialE=; ACTIVITY=1327318848443; recently_watched_video_id_list=fa74e0c9cb6121d125c76409054f90fbWwIAAABzCwAAADZxMU55U3p0R1hjcwsAAABfdGgwcy1McWZFaw==; PREF=f1=50000000&fv=11.1.102";
				$this->download($_POST['wvt_url'], $this->options['videodir'] . $file . '.html', $cookie);
				$url = preg_replace("!http://.*?/!is", "http://r8.svo02s02.c.youtube.com/", str_replace("\u0026", "&", urldecode(preg_replace("!.*itag=34,url=(.*)%3B.*!is", "$1", @file_get_contents($this->options['videodir'] . $file . '.html')))));
				if (strlen($url) < 1000) {
					$this->download($url, $this->options['videodir'] . $file . '.mp4', $cookie, $_POST['wvt_url']);
				}
				@unlink($this->options['videodir'] . $file . '.html');
			} else {
				$this->download($_POST['wvt_url'], $this->options['videodir'] . $file . '.mp4');
			}
			return $file . '.mp4';
		} elseif (!empty($_POST['wvt_path'])) {
			$file = preg_replace("!\.[^\.]+$!is", "" , preg_replace("!.*/!", "", $_POST['wvt_path'])) . '.mp4';
			@copy($_POST['wvt_path'], $this->options['videodir'] . $file);
			return $file;
		}
		return '';
	}
/* download external url */
	function download ($url, $file, $cookie = '', $ref = '') {
		$ch = @curl_init($url);
		$fp = @fopen($file, 'wb+');
		@curl_setopt($ch, CURLOPT_HEADER, 0);
		if ($cookie) {
			@curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		if ($ref) {
			@curl_setopt($ch, CURLOPT_REFERER, $ref);
		}
		@curl_setopt($ch, CURLOPT_USERAGENT, empty($_SERVER['HTTP_USER_AGENT']) ? 'WEBO Video Transmuter' : $_SERVER['HTTP_USER_AGENT']);
		@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		@curl_setopt($ch, CURLOPT_FILE, $fp);
		@curl_exec($ch);
		@curl_close($ch);
		@fclose($fp);
	}
}